<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require_once('includes/application_top.php');
  
  if (file_exists(DIR_WS_LANGUAGES . $language . '/' . FILENAME_NEWSCENTER)) {
    include_once(DIR_WS_LANGUAGES . $language . '/' . FILENAME_NEWSCENTER);
  }

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  
  $newscenter_id = (-1) * $admin['id'];
  $default_image_id = '';
  if(isset($HTTP_GET_VARS['nID']) && !empty($HTTP_GET_VARS['nID'])) {
	  $newscenter_id = $HTTP_GET_VARS['nID'];
	  $default_image_query = tep_db_query("select default_image_id from " . TABLE_NEWSCENTER . " where newscenter_id = " . (int)$newscenter_id);
	  $default_image = tep_db_fetch_array($default_image_query);
	  $default_image_id = $default_image['default_image_id'];
  }

  if (tep_not_null($action)) {
    switch ($action) {
      case 'upload_image':
		$newscenter_image = new upload('fileToUpload');
        $newscenter_image->set_destination(DIR_FS_CATALOG_images . 'newscenter/');
		if ($newscenter_image->parse() && $newscenter_image->save()) {
		  tep_db_query("insert into " . TABLE_NEWSCENTER_images . "(newscenter_id, image) values(" . $newscenter_id . ", '" . tep_db_input($newscenter_image->filename) . "')");
		}
        break;
	  case 'delete_image':
		if(isset($HTTP_GET_VARS['mID']) && !empty($HTTP_GET_VARS['mID'])) {
		  $mID = tep_db_input($HTTP_GET_VARS['mID']);
		  $image_query = tep_db_query('select image from ' . TABLE_NEWSCENTER_images . ' where id = ' . (int)$mID);
		  $image = tep_db_fetch_array($image_query);
		  $image_path = DIR_FS_CATALOG_images . 'newscenter/' . $image['image'];
		  if(file_exists($image_path)) @unlink($image_path);
		  tep_db_query('delete from ' . TABLE_NEWSCENTER_images . ' where id = ' . (int)$mID);
		}
		break;
    }
  }

// check if the catalog image directory exists
  if (is_dir(DIR_FS_CATALOG_images)) {
    if (!tep_is_writable(DIR_FS_CATALOG_images)) $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
  } else {
    $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_DOES_NOT_EXIST, 'error');
  }
  
  $images_sql = tep_db_query("select id, image from " . TABLE_NEWSCENTER_images . " where newscenter_id = " . (int)$newscenter_id);
  
  $no_of_images = tep_db_num_rows($images_sql);
  if($no_of_images > 0) {
?>

    <table border="0" width="100%" cellspacing="0" cellpadding="2" class="imageContent">
      <tr>
<?php
	$rows = 0;
	$images_arr = array();
	while($images = tep_db_fetch_array($images_sql)) {
	  $images_arr[] = array('id' => $images['id'], 'text' => $images['image']);
	  $image_link = HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_images . 'newscenter/' . $images['image'];
	  $rows++;
	  
	  echo '      <td width="33%"><img src="../' . DIR_WS_images . 'newscenter/' . $images['image'] . '" width="200px" /><a href="#" onclick="return ajaxFileDel(' . $images['id'] . ')">' . TEXT_DELETE_IMAGE . '</a><br />' . $image_link . '</td>';
	  if ((($rows / 3) == floor($rows / 3)) && ($rows != $no_of_images)) {
        echo '      </tr>' . "\n";
		//echo '      <tr><td colspan="' . $rows . '">&nbsp;</td></tr>' . "\n";
        echo '      <tr>' . "\n";
      }
	}
?>
    </table>
    <div><span class="imageUploadTitle"><?php echo TEXT_DEFAULT_IMAGE; ?></span>&nbsp;<?php echo tep_draw_pull_down_menu('default_image_id', $images_arr, $default_image_id); ?></div>
<?php
  } else {
	  echo '<i>' . TEXT_NO_images . '</i>';
  }
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
